package com.soft.service.impl;

import java.util.List;

import com.soft.dao.UserDao;
import com.soft.model.User;
import com.soft.service.IUserService;
import com.soft.util.Const;

public class UserServiceImpl implements IUserService {

	@Override
	public boolean add(User user) {
		String sql = "insert into " + Const.USER_TB + " (username,password,usercname,addtime) values (?,?,?,now())";
		String username = user.getUsername();
		String password = user.getPassword();
		String usercname = user.getUsercname();
		String imgname = user.getImgname();
		if(imgname == null) {
			imgname="default.jpg";
		}
		return UserDao.update(sql, username, password, usercname,imgname);
	}

	@Override
	public boolean delete(String id) {
		String sql = "delete from " + Const.USER_TB + " where id=?";
		return UserDao.update(sql, id);
	}

	@Override
	public List<User> findAll() {
		String sql = "select id,username,password,usercname,addtime from " + Const.USER_TB + " order by addtime desc";
		return UserDao.getUser(sql);
	}

	@Override
	public List<User> find(String username, String beginTime, String endTime, int pageSize, String currPage) {
		String sql = "select id,username,password,usercname,addtime from " + Const.USER_TB + " where 1=1";
		StringBuffer sb = new StringBuffer(sql);
		if (username != null && !"".equals(username)) {
			sb.append(" and username like '%" + username + "%'");
		}
		if (beginTime != null && !"".equals(beginTime)) {
			sb.append(" and addtime >='" + beginTime + "'");
		}
		if (endTime != null && !"".equals(endTime)) {
			sb.append(" and addtime <='" + endTime + "'");
		}
		int pageNum = 1;
		if (currPage != null && !"".equals(currPage)) {
			pageNum = Integer.parseInt(currPage);
		}
		sb.append(" order by addtime desc");
		sb.append(" limit ?,?");
		sql = sb.toString();
		// System.out.println(sql);
		int start = (pageNum - 1) * pageSize;
		return UserDao.getUser(sql, start, pageSize);
	}

	@Override
	public User findById(String id) {
		String sql = "select id,username,password,usercname,addtime from " + Const.USER_TB + " where id=?";
		List<User> user_list = UserDao.getUser(sql, id);
		if (user_list != null && user_list.size() > 0) {
			return user_list.get(0);
		}
		return null;
	}

	@Override
	public User findByName(String username) {
		String sql = "select id,username,password,usercname,addtime from " + Const.USER_TB + " where username=?";
		List<User> user_list = UserDao.getUser(sql, username);
		if (user_list != null && user_list.size() == 1) {
			return user_list.get(0);
		}
		return null;
	}

	@Override
	public User findByUsernameAndPassword(String username, String password) {
		String sql = "select id,username,password,usercname,addtime from " + Const.USER_TB
				+ " where username=? and password=?";
		List<User> user_list = UserDao.getUser(sql, username, password);
		if (user_list != null && user_list.size() == 1) {
			return user_list.get(0);
		}
		return null;
	}

	@Override
	public boolean update(User user) {
		int id = user.getId();
		String username = user.getUsername();
		String password = user.getPassword();
		String usercname = user.getUsercname();
		String imgname = user.getImgname();
		String sql = "update " + Const.USER_TB + " set username=?,password=?,usercname=? where id=?";
		return UserDao.update(sql, username, password, usercname, imgname, id);
	}

	@Override
	public int getCount(String username, String beginTime, String endTime) {
		String sql = "select count(id) from " + Const.USER_TB + " where 1=1";
		StringBuffer sb = new StringBuffer(sql);
		if (username != null && !"".equals(username)) {
			sb.append(" and username like '%" + username + "%'");
		}
		if (beginTime != null && !"".equals(beginTime)) {
			sb.append(" and addtime >='" + beginTime + "'");
		}
		if (endTime != null && !"".equals(endTime)) {
			sb.append(" and addtime <='" + endTime + "'");
		}
		sql = sb.toString();
//		System.out.println(sql);
		return UserDao.getCount(sql);
	}

	@Override
	public int getPageCount(String username, String beginTime, String endTime, int pageSize) {
		int count = getCount(username, beginTime, endTime);
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		return pageCount;
	}
}
